package net.dgg.iboss.sc.backservice.task.service;

import com.alibaba.fastjson.JSONObject;
import net.dgg.framework.tac.mq.rabbitmq.queue.DggMessageSendUtil;
import net.dgg.iboss.base.service.CmsService;
import net.dgg.iboss.base.util.DateUtil;
import net.dgg.iboss.sc.backservice.task.constant.ScProductConstant;
import net.dgg.iboss.sc.backservice.task.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 公用Service
 */
@Service
public class CommonService {
    @Autowired
    private CmsService cmsService;


    /**
     * <p>@Description 异步推送消息</p>
     * <p>@author mqy</p>
     *
     * @param userEntityList 推送消息的对象
     * @param msg            消息内容
     * @param templateCode   消息模板的code
     *                       <p>@Time 2018/9/14 0014 9:40 创建时间</p>
     *                       <p>@return</p>
     */
    public void sendMsg(List<UserEntity> userEntityList, String[] msg, String templateCode) {
        for (UserEntity sendUser : userEntityList) {
            cmsService.sendMessage(sendUser.getId(), 1L, msg, templateCode, 1);
        }
    }
    /**
     * 查询字典接口
     *
     * @param code   编码
     * @return
     */
    public Map getTreeBookObj(String code) {
        Map treeBookObj;
        try {
            treeBookObj = cmsService.getTreeBookObj(code);
        }catch (Exception e){
            return null;
        }
        return treeBookObj;
    }
    /**
     * 计算日期 存在暂停时间时
     *
     * @param start          开始时间
     * @param len            天数
     * @param suspendMiniSec 暂停时长 ：毫秒
     * @param dateType       类型，常量定义于 WorkDayOrNaturalDayStatus
     * @return
     */
    public Date getDateFromStartByType(Date start, Integer len, Long suspendMiniSec, int dateType) {
        return ScProductConstant.WORKDAY_STATUS == dateType ?
                this.getWorkDateFrom(start, len, suspendMiniSec) : this.getDateFrom(start, len, suspendMiniSec);
    }
    /**
     * 2018年6月14日 16:30:09
     * 修改  先计算预计完结时间 再按自然日 顺延 暂停总时长
     *
     * @param start
     * @param len
     * @param suspendMiniSec
     * @return
     */
    public Date getWorkDateFrom(Date start, Integer len, Long suspendMiniSec) {
        Date date = this.getWorkDateFrom(start, len);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MILLISECOND, suspendMiniSec.intValue());
        return calendar.getTime();
    }
    /**
     * 获取代入暂停时间后的 从某日开始x个工作日的日期
     *
     * @param start          开始日期
     * @param len            天数
     * @param suspendMiniSec 暂停时长 单位: 毫秒
     * @return
     */
    private Date getDateFrom(Date start, Integer len, Long suspendMiniSec) {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(start);
        Calendar overCalendar = Calendar.getInstance();
        overCalendar.setTime(start);
        overCalendar.add(Calendar.DAY_OF_YEAR, len);
        overCalendar.add(Calendar.MILLISECOND, suspendMiniSec.intValue());
        return overCalendar.getTime();
    }
    /**
     * 获取从某日开始x个工作日的日期
     *
     * @param start 开始日期
     * @param len   天数
     * @return
     */
    public Date getWorkDateFrom(Date start, Integer len) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(start);
        Map map = new HashMap();
        map.put("date", fmt.format(start));
        map.put("days", len);
        String dateStr = this.cmsService.findAfterDate(fmt.format(start), len);
        Assert.hasText(dateStr, "获取工作日失败！");
        Date date = null;
        try {
            date = fmt.parse(dateStr);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date);
            calendar1.set(Calendar.HOUR_OF_DAY, calendarStart.get(Calendar.HOUR_OF_DAY));
            calendar1.set(Calendar.MINUTE, calendarStart.get(Calendar.MINUTE));
            calendar1.set(Calendar.SECOND, calendarStart.get(Calendar.SECOND));
            calendar1.set(Calendar.MILLISECOND, calendarStart.get(Calendar.MILLISECOND));
            return calendar1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            Assert.isTrue(false, "工作日期转换出现异常！");
        }
        return date;
    }
    /**
     * 查询某天是不是工作日
     *
     * @param date
     * @return true:是工作日，false：不是工作日
     */
    public  boolean isWorkDay(Date date) {
        boolean isWeek = true;
            try {
                isWeek = cmsService.findDayIsWorkday(DateUtil.dateToString(date, "yyyy-MM-dd")) == 1;
            } catch (Exception e) {
                return false;
            }
        return isWeek;
    }

    /**
     * 向送企管家发送消息
     * @param orderId
     * @param userId
     * @param userName
     * @param loginName
     * @param content
     */
    public void sendQgjMessage(Long orderId,String businessTypeId,Long userId,String userName,String loginName,String content){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderId",orderId);
        jsonObject.put("businessTypeId",businessTypeId);
        jsonObject.put("content",content);
        jsonObject.put("userId",userId);
        jsonObject.put("userName",userName);
        jsonObject.put("loginName",loginName);
        DggMessageSendUtil.putMessage("net.dgg.iboss.orf.order2.OrderButlerChangeEvent", jsonObject);

    }
}



